package com.violorch.game;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.GridLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by violorch on 4/23/15.
 */
public class GameView extends GridLayout {


    private Card cardMap[][] = new Card[4][4];
    private List<Point> emptyPoint = new ArrayList<>();
    boolean firstAddCard = true;
    public boolean isFirstGame = true;

    public GameView(Context context) {
        super(context);
        intiGridView();
    }

    public GameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        intiGridView();
    }

    public GameView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        intiGridView();
    }

    private void intiGridView() {
        setColumnCount(4);
        setBackgroundColor(0xffbbada0);

        setOnTouchListener(new OnTouchListener() {

            private float startX, startY, offsetX, offsetY;

            @Override
            public boolean onTouch(View view, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        startX = event.getX();
                        startY = event.getY();
                        break;
                    case MotionEvent.ACTION_UP:
                        offsetX = event.getX() - startX;
                        offsetY = event.getY() - startY;

                        if (Math.abs(offsetX) > Math.abs(offsetY)) {
                            if (offsetX < -5) {
                                swipeLeft();
                                checkComplete();

                            } else if (offsetX > 5) {
                                swipeRight();
                                checkComplete();
                            }
                        } else {
                            if (offsetY < -5) {
                                swipeUp();
                                checkComplete();
                            } else if (offsetY > 5) {
                                swipeDown();
                                checkComplete();
                            }
                        }
                        break;
                }

                return true;
            }
        });
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (isFirstGame) {
            isFirstGame = false;
            if (firstAddCard) {
                int cardWidth = (Math.min(w, h) - 10) / 4;
                addCards(cardWidth, cardWidth);
                firstAddCard = false;
            }

            startGame();
        }
    }

    protected void addCards(int cardWidth, int cardHeight) {
        Card c;

        for (int y = 0; y < 4; y ++) {
            for (int x = 0; x < 4 ; x ++) {
                c = new Card(getContext());
                c.setNumber(0);
                addView(c, cardWidth, cardHeight);

                cardMap[x][y] = c;
            }
        }
    }

    private void startGame() {
        intiGridView();
        MainActivity.getMainActivity().clearScore();
        MainActivity.getMainActivity().addScore(0);

        for (int y = 0; y < 4; y ++) {
            for (int x = 0; x < 4; x ++) {
                cardMap[x][y].setNumber(0);
            }
        }
        addRandomNum();
        addRandomNum();
    }

    private void addRandomNum() {
        emptyPoint.clear();

        for (int y = 0; y < 4; y ++) {
            for (int x = 0; x < 4; x ++) {
                if (cardMap[x][y].getNumber() <= 0) {
                    emptyPoint.add(new Point(x, y));
                }
            }
        }

        if (emptyPoint.size() < 1)
            return;

        Point p = emptyPoint.remove((int)(Math.random() * emptyPoint.size()));
        cardMap[p.x][p.y].setNumber(Math.random() > 0.1 ? 2 : 4);
    }

    private void swipeLeft() {
        int flag = 0;

        for (int y = 0; y < 4; y ++) {
            for (int x = 0; x < 4; x ++) {
                for (int x1 = x +1; x1 < 4; x1 ++) {
                    if (cardMap[x1][y].getNumber() > 0) {
                        if (cardMap[x][y].getNumber() <= 0) {
                            cardMap[x][y].setNumber(cardMap[x1][y].getNumber());
                            cardMap[x1][y].setNumber(0);
                            x --;
                            flag ++;
                        } else if (cardMap[x][y].equals(cardMap[x1][y])){
                            cardMap[x][y].setNumber(cardMap[x][y].getNumber() * 2);
                            cardMap[x1][y].setNumber(0);

                            MainActivity.getMainActivity().addScore(cardMap[x][y].getNumber());
                            flag ++;
                        }
                        break;
                    }
                }
            }
        }
        if (flag > 0) addRandomNum();
    }

    private void swipeRight() {
        int flag = 0;

        for (int y = 0; y < 4; y ++) {
            for (int x = 3; x >= 0; x --) {
                for (int x1 = x -1; x1 >= 0; x1 --) {
                    if (cardMap[x1][y].getNumber() > 0) {
                        if (cardMap[x][y].getNumber() <= 0) {
                            cardMap[x][y].setNumber(cardMap[x1][y].getNumber());
                            cardMap[x1][y].setNumber(0);

                            x ++;
                            flag ++;
                        } else if (cardMap[x][y].equals(cardMap[x1][y])){
                            cardMap[x][y].setNumber(cardMap[x][y].getNumber() * 2);
                            cardMap[x1][y].setNumber(0);
                            MainActivity.getMainActivity().addScore(cardMap[x][y].getNumber());
                            flag ++;
                        }
                        break;
                    }
                }
            }
        }
        if (flag > 0) addRandomNum();
    }

    private void swipeUp() {
        int flag = 0;

        for (int x = 0; x < 4; x ++) {
            for (int y = 0; y < 4; y ++) {
                for (int y1 = y +1; y1 < 4; y1 ++) {
                    if (cardMap[x][y1].getNumber() > 0) {
                        if (cardMap[x][y].getNumber() <= 0) {
                            cardMap[x][y].setNumber(cardMap[x][y1].getNumber());
                            cardMap[x][y1].setNumber(0);

                            y --;
                            flag ++;
                        } else if (cardMap[x][y].equals(cardMap[x][y1])){
                            cardMap[x][y].setNumber(cardMap[x][y].getNumber() * 2);
                            cardMap[x][y1].setNumber(0);
                            MainActivity.getMainActivity().addScore(cardMap[x][y].getNumber());
                            flag ++;
                        }
                        break;
                    }
                }
            }
        }
        if (flag > 0) addRandomNum();
    }

    private void swipeDown() {
        int flag = 0;

        for (int x = 0; x < 4; x ++) {
            for (int y = 3; y >= 0; y --) {
                for (int y1 = y -1; y1 >= 0; y1 --) {
                    if (cardMap[x][y1].getNumber() > 0) {
                        if (cardMap[x][y].getNumber() <= 0) {
                            cardMap[x][y].setNumber(cardMap[x][y1].getNumber());
                            cardMap[x][y1].setNumber(0);

                            y ++;
                            flag ++;
                        } else if (cardMap[x][y].equals(cardMap[x][y1])){
                            cardMap[x][y].setNumber(cardMap[x][y].getNumber() * 2);
                            cardMap[x][y1].setNumber(0);
                            MainActivity.getMainActivity().addScore(cardMap[x][y].getNumber());
                            flag ++;
                        }
                        break;
                    }
                }
            }
        }
        if (flag > 0) addRandomNum();
    }

    private void checkComplete() {
        boolean flag = true;
        ALL:
        for (int y = 0; y < 4; y ++) {
            for (int x = 0; x < 4; x++) {
                if (cardMap[x][y].getNumber() == 0 ||
                        (x > 0 && cardMap[x][y].equals(cardMap[x-1][y])) ||
                        (x < 3 && cardMap[x][y].equals(cardMap[x+1][y])) ||
                        (y < 3 && cardMap[x][y].equals(cardMap[x][y+1])) ||
                        (y > 0 && cardMap[x][y].equals(cardMap[x][y-1]))) {
                    flag = false;
                    break ALL;
                }
            }
        }

        if (flag) {
            new AlertDialog.Builder(getContext()).setTitle("Hi, man")
                    .setMessage("Game Over")
                    .setPositiveButton("Try Again", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            isFirstGame = true;
                            startGame();
                        }
                    }).show();
        }
    }
}
